/*
 * Copyright (c) 2005-2007,2009,2011 Apple Inc. All Rights Reserved.
 *
 * @APPLE_LICENSE_HEADER_START@
 * 
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this
 * file.
 * 
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 * 
 * @APPLE_LICENSE_HEADER_END@
 *
 * 01DL_CreateReleation.c
 */

#include <Security/cssmapi.h>
#include <Security/SecCertificate.h>
#include <Security/SecKeychain.h>
#include <Security/SecKeychainItem.h>

#include "keychain_regressions.h"
#include "kc-helpers.h"

/* Cert File Name: keybank_v3.101.cer */
static const uint8 keybank_der_bytes[] =
{
    0x30, 0x82, 0x03, 0x86, 0x30, 0x82, 0x02, 0xef,
    0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x10, 0x78,
    0xee, 0x48, 0xde, 0x18, 0x5b, 0x20, 0x71, 0xc9,
    0xc9, 0xc3, 0xb5, 0x1d, 0x7b, 0xdd, 0xc1, 0x30,
    0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
    0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x30, 0x5f,
    0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
    0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x17, 0x30,
    0x15, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x0e,
    0x56, 0x65, 0x72, 0x69, 0x53, 0x69, 0x67, 0x6e,
    0x2c, 0x20, 0x49, 0x6e, 0x63, 0x2e, 0x31, 0x37,
    0x30, 0x35, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x13,
    0x2e, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x20, 0x33,
    0x20, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x20,
    0x50, 0x72, 0x69, 0x6d, 0x61, 0x72, 0x79, 0x20,
    0x43, 0x65, 0x72, 0x74, 0x69, 0x66, 0x69, 0x63,
    0x61, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x41, 0x75,
    0x74, 0x68, 0x6f, 0x72, 0x69, 0x74, 0x79, 0x30,
    0x1e, 0x17, 0x0d, 0x39, 0x37, 0x30, 0x34, 0x31,
    0x37, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a,
    0x17, 0x0d, 0x31, 0x31, 0x31, 0x30, 0x32, 0x34,
    0x32, 0x33, 0x35, 0x39, 0x35, 0x39, 0x5a, 0x30,
    0x81, 0xba, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x03,
    0x55, 0x04, 0x0a, 0x13, 0x16, 0x56, 0x65, 0x72,
    0x69, 0x53, 0x69, 0x67, 0x6e, 0x20, 0x54, 0x72,
    0x75, 0x73, 0x74, 0x20, 0x4e, 0x65, 0x74, 0x77,
    0x6f, 0x72, 0x6b, 0x31, 0x17, 0x30, 0x15, 0x06,
    0x03, 0x55, 0x04, 0x0b, 0x13, 0x0e, 0x56, 0x65,
    0x72, 0x69, 0x53, 0x69, 0x67, 0x6e, 0x2c, 0x20,
    0x49, 0x6e, 0x63, 0x2e, 0x31, 0x33, 0x30, 0x31,
    0x06, 0x03, 0x55, 0x04, 0x0b, 0x13, 0x2a, 0x56,
    0x65, 0x72, 0x69, 0x53, 0x69, 0x67, 0x6e, 0x20,
    0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x74,
    0x69, 0x6f, 0x6e, 0x61, 0x6c, 0x20, 0x53, 0x65,
    0x72, 0x76, 0x65, 0x72, 0x20, 0x43, 0x41, 0x20,
    0x2d, 0x20, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x20,
    0x33, 0x31, 0x49, 0x30, 0x47, 0x06, 0x03, 0x55,
    0x04, 0x0b, 0x13, 0x40, 0x77, 0x77, 0x77, 0x2e,
    0x76, 0x65, 0x72, 0x69, 0x73, 0x69, 0x67, 0x6e,
    0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x43, 0x50, 0x53,
    0x20, 0x49, 0x6e, 0x63, 0x6f, 0x72, 0x70, 0x2e,
    0x62, 0x79, 0x20, 0x52, 0x65, 0x66, 0x2e, 0x20,
    0x4c, 0x49, 0x41, 0x42, 0x49, 0x4c, 0x49, 0x54,
    0x59, 0x20, 0x4c, 0x54, 0x44, 0x2e, 0x28, 0x63,
    0x29, 0x39, 0x37, 0x20, 0x56, 0x65, 0x72, 0x69,
    0x53, 0x69, 0x67, 0x6e, 0x30, 0x81, 0x9f, 0x30,
    0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
    0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x81,
    0x8d, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81,
    0x00, 0xd8, 0x82, 0x80, 0xe8, 0xd6, 0x19, 0x02,
    0x7d, 0x1f, 0x85, 0x18, 0x39, 0x25, 0xa2, 0x65,
    0x2b, 0xe1, 0xbf, 0xd4, 0x05, 0xd3, 0xbc, 0xe6,
    0x36, 0x3b, 0xaa, 0xf0, 0x4c, 0x6c, 0x5b, 0xb6,
    0xe7, 0xaa, 0x3c, 0x73, 0x45, 0x55, 0xb2, 0xf1,
    0xbd, 0xea, 0x97, 0x42, 0xed, 0x9a, 0x34, 0x0a,
    0x15, 0xd4, 0xa9, 0x5c, 0xf5, 0x40, 0x25, 0xdd,
    0xd9, 0x07, 0xc1, 0x32, 0xb2, 0x75, 0x6c, 0xc4,
    0xca, 0xbb, 0xa3, 0xfe, 0x56, 0x27, 0x71, 0x43,
    0xaa, 0x63, 0xf5, 0x30, 0x3e, 0x93, 0x28, 0xe5,
    0xfa, 0xf1, 0x09, 0x3b, 0xf3, 0xb7, 0x4d, 0x4e,
    0x39, 0xf7, 0x5c, 0x49, 0x5a, 0xb8, 0xc1, 0x1d,
    0xd3, 0xb2, 0x8a, 0xfe, 0x70, 0x30, 0x95, 0x42,
    0xcb, 0xfe, 0x2b, 0x51, 0x8b, 0x5a, 0x3c, 0x3a,
    0xf9, 0x22, 0x4f, 0x90, 0xb2, 0x02, 0xa7, 0x53,
    0x9c, 0x4f, 0x34, 0xe7, 0xab, 0x04, 0xb2, 0x7b,
    0x6f, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x81,
    0xe6, 0x30, 0x81, 0xe3, 0x30, 0x0f, 0x06, 0x03,
    0x55, 0x1d, 0x13, 0x04, 0x08, 0x30, 0x06, 0x01,
    0x01, 0xff, 0x02, 0x01, 0x00, 0x30, 0x44, 0x06,
    0x03, 0x55, 0x1d, 0x20, 0x04, 0x3d, 0x30, 0x3b,
    0x30, 0x39, 0x06, 0x0b, 0x60, 0x86, 0x48, 0x01,
    0x86, 0xf8, 0x45, 0x01, 0x07, 0x01, 0x01, 0x30,
    0x2a, 0x30, 0x28, 0x06, 0x08, 0x2b, 0x06, 0x01,
    0x05, 0x05, 0x07, 0x02, 0x01, 0x16, 0x1c, 0x68,
    0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x77,
    0x77, 0x77, 0x2e, 0x76, 0x65, 0x72, 0x69, 0x73,
    0x69, 0x67, 0x6e, 0x2e, 0x63, 0x6f, 0x6d, 0x2f,
    0x43, 0x50, 0x53, 0x30, 0x34, 0x06, 0x03, 0x55,
    0x1d, 0x1f, 0x04, 0x2d, 0x30, 0x2b, 0x30, 0x29,
    0xa0, 0x27, 0xa0, 0x25, 0x86, 0x23, 0x68, 0x74,
    0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x63, 0x72, 0x6c,
    0x2e, 0x76, 0x65, 0x72, 0x69, 0x73, 0x69, 0x67,
    0x6e, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x70, 0x63,
    0x61, 0x33, 0x2d, 0x67, 0x32, 0x2e, 0x63, 0x72,
    0x6c, 0x30, 0x34, 0x06, 0x03, 0x55, 0x1d, 0x25,
    0x04, 0x2d, 0x30, 0x2b, 0x06, 0x08, 0x2b, 0x06,
    0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08,
    0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
    0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x86, 0xf8,
    0x42, 0x04, 0x01, 0x06, 0x0a, 0x60, 0x86, 0x48,
    0x01, 0x86, 0xf8, 0x45, 0x01, 0x08, 0x01, 0x30,
    0x0b, 0x06, 0x03, 0x55, 0x1d, 0x0f, 0x04, 0x04,
    0x03, 0x02, 0x01, 0x06, 0x30, 0x11, 0x06, 0x09,
    0x60, 0x86, 0x48, 0x01, 0x86, 0xf8, 0x42, 0x01,
    0x01, 0x04, 0x04, 0x03, 0x02, 0x01, 0x06, 0x30,
    0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
    0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x03, 0x81,
    0x81, 0x00, 0x23, 0x5d, 0xee, 0xa6, 0x24, 0x05,
    0xfd, 0x76, 0xd3, 0x6a, 0x1a, 0xd6, 0xba, 0x46,
    0x06, 0xaa, 0x6a, 0x0f, 0x03, 0x90, 0x66, 0xb2,
    0xb0, 0xa6, 0xc2, 0x9e, 0xc9, 0x1e, 0xa3, 0x55,
    0x53, 0xaf, 0x3e, 0x45, 0xfd, 0xdc, 0x8c, 0x27,
    0xdd, 0x53, 0x38, 0x09, 0xbb, 0x7c, 0x4b, 0x2b,
    0xba, 0x95, 0x4a, 0xfe, 0x70, 0x4e, 0x1b, 0x69,
    0xd6, 0x3c, 0xf7, 0x4f, 0x07, 0xc5, 0xf2, 0x17,
    0x5a, 0x4c, 0xa2, 0x8f, 0xac, 0x0b, 0x8a, 0x06,
    0xdb, 0xb9, 0xd4, 0x6b, 0xc5, 0x1d, 0x58, 0xda,
    0x17, 0x52, 0xe3, 0x21, 0xf1, 0xd2, 0xd7, 0x5a,
    0xd5, 0xe5, 0xab, 0x59, 0x7b, 0x21, 0x7a, 0x86,
    0x6a, 0xd4, 0xfe, 0x17, 0x11, 0x3a, 0x53, 0x0d,
    0x9c, 0x60, 0xa0, 0x4a, 0xd9, 0x5e, 0xe4, 0x1d,
    0x0c, 0x29, 0xaa, 0x13, 0x07, 0x65, 0x86, 0x1f,
    0xbf, 0xb4, 0xc9, 0x82, 0x53, 0x9c, 0x2c, 0x02,
    0x8f, 0x23
}; 
static const CSSM_DATA keybank_der =
{
	sizeof(keybank_der_bytes),
	(uint8 *)keybank_der_bytes
};

static void
certTests(SecKeychainRef keychain)
{
	SecCertificateRef certificate = NULL;
	ok_status(SecCertificateCreateFromData(&keybank_der,
		CSSM_CERT_X_509v3, CSSM_CERT_ENCODING_DER, &certificate),
		"SecCertificateCreateFromData");
	ok(certificate, "certificate non NULL");
	ok_status(SecCertificateAddToKeychain(certificate, keychain),
		"SecCertificateAddToKeychain fails unless 4039735 is fixed");

	is_status(SecCertificateAddToKeychain(certificate, keychain),
		errSecDuplicateItem, "SecCertificateAddToKeychain twice");

	SecCertificateRef certificate2 = NULL;
	ok_status(SecCertificateCreateFromData(&keybank_der,
		CSSM_CERT_X_509v3, CSSM_CERT_ENCODING_DER, &certificate2),
		"SecCertificateCreateFromData");
	is_status(SecCertificateAddToKeychain(certificate2, keychain),
		errSecDuplicateItem, "SecCertificateAddToKeychain twice");
	is(CFGetRetainCount(certificate2), 1, "certificate2 retain count is 1");
	CFRelease(certificate2);

	SecKeychainRef certKeychain = NULL;
	ok_status(SecKeychainItemCopyKeychain((SecKeychainItemRef)certificate,
		&certKeychain), "SecKeychainItemCopyKeychain");
	is((intptr_t)keychain, (intptr_t)certKeychain, "cert's keychain is keychain");
	CFRelease(certKeychain);

	is(CFGetRetainCount(certificate), 1, "certificate retain count is 1");
	CFRelease(certificate);

	ok_status(SecKeychainDelete(keychain), "SecKeychainDelete");
	cmp_ok(CFGetRetainCount(keychain), >=, 1, "keychain retain count is 1");
	CFRelease(keychain);
}

int
kc_10_item_add_certificate(int argc, char * const *argv)
{
	plan_tests(33);

	/* Test with autocommit on. */
	SecKeychainRef keychain = createNewKeychain("test.keychain", "test");
	ok(keychain, "keychain non NULL");
	certTests(keychain);

	/* Test with autocommit off. */
	keychain = NULL;
	keychain = createNewKeychain("test2.keychain", "test");
	ok(keychain, "keychain non NULL");
	CSSM_DL_DB_HANDLE cspdl_dldb = {};
	ok_status(SecKeychainGetDLDBHandle(keychain, &cspdl_dldb),
		"SecKeychainGetDLDBHandle");
	CSSM_DL_DB_HANDLE dldb = {};
	ok_status(CSSM_DL_PassThrough(cspdl_dldb, CSSM_APPLECSPDL_DB_GET_HANDLE,
		NULL, (void **)&dldb), "get dl handle");
	ok(dldb.DLHandle, "dldb.DLHandle non 0");
	ok(dldb.DBHandle, "dldb.DBHandle non 0");
	ok_status(CSSM_DL_PassThrough(dldb, CSSM_APPLEFILEDL_TOGGLE_AUTOCOMMIT,
		(const void *)FALSE, NULL), "autocommit off");
	certTests(keychain);

    deleteTestFiles();
    return 0;
}
